﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BitTorrentSharp.Peers
{
	/// <summary>
	/// Represent as a message to tell the other side that this side have a piece of data.
	/// </summary>
	public class HaveMessage : PeerNonKeepAliveMessage
	{
		/// <summary>
		/// Get or set the index of the piece the peer owns.
		/// </summary>
		public int PieceIndex
		{
			get;
			set;
		}

		/// <summary>
		/// Get the type of the message. This property always returns <see cref="PeerMessageType.Have"/>.
		/// </summary>
		public override PeerMessageType Type
		{
			get
			{
				return PeerMessageType.Have;
			}
		}
	
		/// <summary>
		/// Get the data sequence of this message.
		/// </summary>
		/// <returns>The data sequence of this message.</returns>
		protected override byte[] GetPayloadBytes()
		{
			return BitConverter.GetBytes(this.PieceIndex).EnsureBigEndian();
		}

		/// <summary>
		/// Initlize of a new instance of the message with the specified piece index.
		/// </summary>
		/// <param name="pieceIndex">The piece index value.</param>
		public HaveMessage(int pieceIndex)
		{
			this.PieceIndex = pieceIndex;
		}

		/// <summary>
		/// Initlize of a new instance of the message with the index set to zero.
		/// </summary>
		public HaveMessage()
			: this(0)
		{
		}
	}
}
